<?php
// +----------------------------------------------------------------------
// | 十分彩网系统 [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2014 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Dean <zxxjjforever@163.com>
// +----------------------------------------------------------------------
namespace Wechat\Controller;
use Common\Controller\HomebaseController;
/**
 * 首页
 */
class LicenseController extends HomebaseController {
	
	public $userInfo;
	public $wxId;
	public $appId;
	public $appSecret;
	public $token;
	
	
	function _initialize() {
		parent::_initialize();
		$this->appId = C('APPID');
		//只要用户一访问此模块，就登录授权，获取用户信息
		$this->userInfo = $this->getWxUserInfo();
	}
	
	
	public function getWxUserInfo($url = '') {
		$user = session('user');//先看看本地session里是否存在微信唯一标记，
		if (!empty($user)) {
			$this->wxId = $user['openid'];//获取用户的openid
			return $user;
		}
	
		//获取授权临时票据（code）
		$code = $_GET['code'];
		if (empty($code)) {
			if (empty($url)) {
				$url = rtrim("http://".$_SERVER['HTTP_HOST'].$_SERVER['HTTP_ORIGIN'].__APP__."?".$_SERVER['QUERY_STRING'], '/');
				redirect($this->getOAuthUrl($url));
			}
		}
		/***************这里开始第二步：通过code获取access_token****************/
		vendor('Wechat.WxUser');
		$openid = session('user.openid');
		if(empty($openid)){
			$weixin = new \weixin_user(C('APPID'), C('APPSECRET'));
			$arr = $weixin->get_token($code);
			$where['openid']=array('eq',$arr['openid']);
			$where['status']=array('eq',"1");
			$openid = $arr['openid'];
			$access_token = $arr['access_token'];
			$token1 = M('token')->where($where)->count();
			if(!empty($token1)){
				$arr['create_time'] = date("Y-m-d H:i:s");
				$arr['status']="1";
				M('token')->where($where)->save($arr);
			}else{
				M('token')->add($arr);
			}
		}
		$wxuser = $weixin->get_user_info($openid, $access_token);
		$mem = M('member')->where(array("openid"=>$openid))->find();
		if(!empty($mem) && is_array($mem)){
			session('user',$mem);
		}else{
			$user['openid'] = $wxuser['openid'];
			$user['nickname'] = $wxuser['nickname'];	
			$user['sex'] = $wxuser['sex'];	
			$user['province'] = $wxuser['province'];	
			$user['city'] = $wxuser['city'];	
			$user['country'] = $wxuser['country'];	
			$user['headimgurl'] = $wxuser['headimgurl'];
			$user['privilege'] = $wxuser['privilege'];
			$user['unionid'] = $wxuser['unionid'];
			M('member')->add($user);
			$mem2 = M('member')->where(array("openid"=>$openid))->find();
			session('user',$mem2);
		}
	}
	
	/**
	 * 获取微信授权url
	 * @param string 授权后跳转的URL
	 * @param bool 是否只获取openid，true时，不会弹出授权页面，但只能获取用户的openid，而false时，弹出授权页面，可以通过openid获取用户信息
	 *
	 */
	public function getOAuthUrl($redirectUrl, $openIdOnly, $state = '') {
		$redirectUrl = urlencode($redirectUrl);
		$scope = $openIdOnly ? 'snsapi_base' : 'snsapi_userinfo';
		$oAuthUrl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$this->appId}&redirect_uri={$redirectUrl}&response_type=code&scope={$scope}&state=1#wechat_redirect";
		return $oAuthUrl;
	}
}
?>